﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AccidentCards
{
    public partial class VehicleForm : Form
    {
        private long accidentCardId;
        private long vehicleCardId;
        public VehicleForm(long _accidentCardId)
        {
            InitializeComponent();
            accidentCardId = _accidentCardId;
            vehicleCardId = -1;
        }

        public VehicleForm(long _accidentCardId, long _vehicleId)
        {
            InitializeComponent();
            accidentCardId = _accidentCardId;
            vehicleCardId = _vehicleId;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (vehicleCardId == -1)
            {
                VehicleCard.CreateVehicleCard(ref vehicleCardId, accidentCardId);
            }
            
            VehicleCard.UpdateCard( vehicleCardId,
                                             enginNumber.Text,              
                                             bodyNumber.Text,                 
                                             chassisNumber.Text,
                                            Convert.ToInt32(creationYear.Text),
                                            Convert.ToInt32(passangersAmmount.Text),
                                            Convert.ToInt32(trailerAmmount.Text),                 
                                             carIdNumber.Text,                   
                                             carRegistrationCardNumber.Text,
                                             vehicleNumber.Text,

                                            Convert.ToInt32(escape.SelectedValue),
                                            Convert.ToInt32(vehicleType.SelectedValue),
                                            Convert.ToInt32( color.SelectedValue),
                                            Convert.ToInt32( malfunctions.SelectedValue),
                                            Convert.ToInt32( factoryCode.SelectedValue),
                                            Convert.ToInt32( modelCode.SelectedValue),
                                            Convert.ToInt32( wheelAndDrive.SelectedValue),
                                            Convert.ToInt32( damagePlaces.SelectedValue),
                                            Convert.ToInt32( registrationRegion.SelectedValue),
                                            Convert.ToInt32( organizationType.SelectedValue),
                                            Convert.ToInt32( ownershipType.SelectedValue),
                                            Convert.ToInt32( organizationName.SelectedValue),
                                            Convert.ToInt32( haveLicence.SelectedValue),
                                            inSearch.Checked );


            this.Close();
        }

        private void LoadComboBoxes()
        {            
            Dictionary.LoadComboBoxValues( escape, Dictionary.DictCategories.VehicleEscapeType);
            Dictionary.LoadComboBoxValues( vehicleType, Dictionary.DictCategories.VehicleType);
            Dictionary.LoadComboBoxValues( color, Dictionary.DictCategories.Color);
            Dictionary.LoadComboBoxValues( malfunctions, Dictionary.DictCategories.Malfunctions);
            Dictionary.LoadComboBoxValues( factoryCode, Dictionary.DictCategories.FactoryCode);
            Dictionary.LoadComboBoxValues( modelCode, Dictionary.DictCategories.ModelCode);
            Dictionary.LoadComboBoxValues( wheelAndDrive, Dictionary.DictCategories.WheelAndDrive);
            Dictionary.LoadComboBoxValues( damagePlaces, Dictionary.DictCategories.DamagePlaces);
            Dictionary.LoadComboBoxValues( registrationRegion, Dictionary.DictCategories.RegistrationRegion);
            Dictionary.LoadComboBoxValues( organizationType, Dictionary.DictCategories.OrganizationType);
            Dictionary.LoadComboBoxValues( ownershipType, Dictionary.DictCategories.OwnershipType);
            Dictionary.LoadComboBoxValues( organizationName, Dictionary.DictCategories.OrganizationName);
            Dictionary.LoadComboBoxValues( haveLicence, Dictionary.DictCategories.HaveLicence);
        }

        private void VehicleForm_Load(object sender, EventArgs e)
        {
            LoadComboBoxes();
            if (vehicleCardId != -1)
            {
                try
                {
                    DataTable dtCard;
                    dtCard = VehicleCard.GetInfoById(vehicleCardId);

                    enginNumber.Text = dtCard.Rows[0]["enginNumber"].ToString();          
                     bodyNumber.Text = dtCard.Rows[0]["bodyNumber"].ToString();                
                     chassisNumber.Text = dtCard.Rows[0]["chassisNumber"].ToString();
                     creationYear.Text = dtCard.Rows[0]["creationYear"].ToString();
                     passangersAmmount.Text = dtCard.Rows[0]["passangersAmmount"].ToString();
                     trailerAmmount.Text = dtCard.Rows[0]["trailerAmmount"].ToString();               
                     carIdNumber.Text = dtCard.Rows[0]["carIdNumber"].ToString();                 
                     carRegistrationCardNumber.Text = dtCard.Rows[0]["carRegistrationCardNumber"].ToString();
                     vehicleNumber.Text = dtCard.Rows[0]["vehicleNumber"].ToString();

                  escape.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["escape"]);
                    vehicleType.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["vehicleType"]);
                    color.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["color"]);
                   malfunctions.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["malfunctions"]);
                    factoryCode.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["factoryCode"]);
                    modelCode.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["modelCode"]);
                    wheelAndDrive.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["wheelAndDrive"]);
                    damagePlaces.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["damagePlaces"]);
                    registrationRegion.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["registrationRegion"]);
                    organizationType.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["organizationType"]);
                    ownershipType.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["ownershipType"]);
                    organizationName.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["organizationName"]);
                    haveLicence.SelectedValue= Convert.ToInt32(dtCard.Rows[0]["haveLicence"]);
                    inSearch.Checked = Convert.ToBoolean( dtCard.Rows[0]["inSearch"] );
                    
                }
                catch
                {
                    MessageBox.Show("Ошибка в  LoadVehicleForm");
                }
            }

        }


    }
}
